package com.whf.android.jar.util.color;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Stone
 * @version V1.0
 * @description: 产生随机的颜色值
 * @date 10/24/18 14:59
 * @email redsstone@163.com
 */
public class ColorCreate {

    /**
     * 创建速记HSL
     *
     * @return
     */
    private ColorHSL createHsl() {

        double h = Math.random();
        double s = Math.random();
        double l = Math.random();
        return new ColorHSL(h, s, l);
    }

    private double hue2Rgb(double p, double q, double t) {

        if (t < 0) {
            t += 1;
        }

        if (t > 1) {
            t -= 1;
        }

        if (t < 1.0 / 6) {
            return p + (q - p) * 6 * t;
        }

        if (t < 1.0 / 2) {
            return q;
        }

        if (t < 2.0 / 3) {
            return p + (q - p) * (2.0 / 3 - t) * 6;
        }
        return p;
    }

    protected ColorRGB hslToRgb(double h, double s, double l) {
        double r, g, b;
        if (s == 0) {
            // 饱和度为0 为灰色
            r = g = b = l;
        } else {

            double q = l < 0.5 ? l * (1 + s) : l + s - l * s;
            double p = 2 * l - q;

            r = hue2Rgb(p, q, h + 1.0 / 3);
            g = hue2Rgb(p, q, h);
            b = hue2Rgb(p, q, h - 1.0 / 3);
        }
        r = Math.round(r * 255);
        g = Math.round(g * 255);
        b = Math.round(b * 255);

        return new ColorRGB((int) r, (int) g, (int) b);
    }

    /**
     * 创建多个颜色值
     *
     * @param count
     * @return
     */
    protected List<ColorRGB> createColorRGBs(int count) {

        List<ColorRGB> colorRGBList = new ArrayList<>();
        List<ColorHSL> list = getHslArray(count);
        for (int i = 0; i < list.size(); i++) {
            ColorHSL colorHSL = list.get(i);
            ColorRGB rgb = hslToRgb(colorHSL.getH(), colorHSL.getS(), colorHSL.getL());
            colorRGBList.add(rgb);
        }

        return colorRGBList;

    }

    /**
     * 转换成16进制颜色值
     *
     * @param count
     * @return
     */
    protected List<String> creatColorStringHx(int count) {
        return null;
    }


    private List<ColorHSL> getHslArray(int count) {
        List<ColorHSL> list = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            ColorHSL colorHSL = createHsl();
            if (i > 0 && Math.abs(colorHSL.getH() - list.get(i - 1).getH()) < 0.25) {
                i--;
                continue;
            }

            // [0.7 - 0.9] 排除过灰颜色
            colorHSL.setS(0.7 + (colorHSL.getS() * 0.2));

            // [0.4 - 0.8] 排除过亮过暗色
            colorHSL.setL(0.4 + (colorHSL.getL() * 0.4));

            // 数据转化到小数点后两位
            DecimalFormat df = new DecimalFormat("######0.00");

            colorHSL.setH(Double.parseDouble(df.format(colorHSL.getH())));
            colorHSL.setS(Double.parseDouble(df.format(colorHSL.getS())));
            colorHSL.setL(Double.parseDouble(df.format(colorHSL.getL())));
            list.add(colorHSL);
        }

        return list;
    }


}
